package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import edu.scau.divineplace.entity.dos.*;
import edu.scau.divineplace.entity.enums.CommentTargetTypeEnum;
import edu.scau.divineplace.entity.enums.IsReadEnum;
import edu.scau.divineplace.entity.enums.LikeTargetTypeEnum;
import edu.scau.divineplace.entity.query.PageQuery;
import edu.scau.divineplace.entity.vo.message.LikeMessageVO;
import edu.scau.divineplace.entity.vo.message.ReplyMessageVO;
import edu.scau.divineplace.util.PageQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:48
 */
@Service
public class MessageService {

    @Autowired
    private ImportMapperService importMapperService;

    /**
     * 通过用户id获取该用户的被回复消息
     * @param userId
     * @param pageQuery
     * @return
     */
    public List<ReplyMessageVO> listReplyMessage(Long userId, PageQuery pageQuery) {
        // 从commentForum里面获取被回复的消息，被回复人是userId，且不能是userId发的
        Preconditions.checkNotNull(userId);
        PageQueryUtil.fillAllIfAbsent(pageQuery);
        // 根据is_read排序一下
        LambdaQueryWrapper<CommentForumDO> commentQuery = Wrappers.<CommentForumDO>lambdaQuery()
                .eq(CommentForumDO::getReplyUserId, userId)
                .ne(CommentForumDO::getUserId, userId)
                .orderByAsc(CommentForumDO::getIsRead)
                .orderByDesc(CommentForumDO::getUpdateTime);
        Page<CommentForumDO> commentPage = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        List<CommentForumDO> commentDOS = importMapperService.commentForumMapper.selectPage(commentPage, commentQuery).getRecords();
        List<ReplyMessageVO> replyMessageVOS = Lists.newArrayList();
        List<Long> messageIds = Lists.newArrayList();
        // 获取评论用户的信息
        for (CommentForumDO commentDO : commentDOS) {
            messageIds.add(commentDO.getId());
            UserDO userDO = importMapperService.userMapper.selectById(commentDO.getUserId());
            CommentForumDO repliedCommentDO = importMapperService.commentForumMapper.selectById(commentDO.getReplyCommentForumId());
            ReplyMessageVO VO = ReplyMessageVO.builder()
                    .userId(userDO.getId())
                    .userName(userDO.getName())
                    .userAvatar(userDO.getAvatar())
                    .commentId(commentDO.getId())
                    .parentId(commentDO.getParentId())
                    .commentTime(commentDO.getCreateTime())
                    .commentTargetId(commentDO.getCommentTargetId())
                    .commentTargetType(commentDO.getCommentTargetType())
                    .comment(commentDO.getComment())
                    .repliedComment(repliedCommentDO.getComment())
                    .build();
            replyMessageVOS.add(VO);
        }
        // 把消息设为已读
        LambdaUpdateWrapper<CommentForumDO> commentForumUpdateWrapper = Wrappers.<CommentForumDO>lambdaUpdate()
                .set(CommentForumDO::getIsRead, IsReadEnum.IS_READ_TRUE.getType())
                .in(CommentForumDO::getId, messageIds);
        importMapperService.commentForumMapper.update(null, commentForumUpdateWrapper);
        return replyMessageVOS;
    }

    /**
     * 通过用户id获取该用户的点赞消息
     * @param userId
     * @param pageQuery
     * @return
     */
    public List<LikeMessageVO> listLikeMessage(Long userId, PageQuery pageQuery) {
        Preconditions.checkNotNull(userId);
        PageQueryUtil.fillAllIfAbsent(pageQuery);
        // 根据is_read排序一下
        LambdaQueryWrapper<LikeDO> likeQuery = Wrappers.<LikeDO>lambdaQuery()
                .eq(LikeDO::getLikeTargetUserId, userId)
                .orderByAsc(LikeDO::getIsRead)
                .orderByDesc(LikeDO::getUpdateTime)
                .orderByDesc(LikeDO::getCreateTime);
        Page<LikeDO> likePage = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        List<LikeDO> likeDOS = importMapperService.likeMapper.selectPage(likePage, likeQuery).getRecords();
        List<LikeMessageVO> likeMessageVOS = Lists.newArrayList();
        List<Long> likeIds = Lists.newArrayList();
        // 根据点赞的内容的类型，从不同的表中获取点赞的内容和点赞者的信息
        for (LikeDO likeDO : likeDOS) {
            likeIds.add(likeDO.getId());
            // 获取点赞的内容
            Long likeTargetId = likeDO.getLikeTargetId();
            LikeTargetTypeEnum likeTargetType = likeDO.getLikeTargetType();
            String comment;
            Long commentTargetId = null;
            CommentTargetTypeEnum commentTargetTypeEnum = null;
            switch (likeTargetType) {
                case COMMENT_SCORE:
                    CommentScoreDO commentScoreDO = importMapperService.commentScoreMapper.selectById(likeTargetId);
                    comment = commentScoreDO.getComment();
                    commentTargetId = commentScoreDO.getContentId();
                    break;
                case COMMENT_FORUM:
                    CommentForumDO commentForumDO = importMapperService.commentForumMapper.selectById(likeTargetId);
                    comment = commentForumDO.getComment();
                    commentTargetId = commentForumDO.getCommentTargetId();
                    commentTargetTypeEnum = commentForumDO.getCommentTargetType();
                    break;
                case JOURNAL:
                    JournalDO journalDO = importMapperService.journalMapper.selectById(likeTargetId);
                    comment = journalDO.getComment();
                    break;
                default: throw new RuntimeException("枚举异常");
            }
            // 获取点赞者信息
            UserDO userDO = importMapperService.userMapper.selectById(likeDO.getUserId());
            LikeMessageVO VO = LikeMessageVO.builder()
                    .userId(userDO.getId())
                    .userAvatar(userDO.getAvatar())
                    .userName(userDO.getName())
                    .likeTime(likeDO.getCreateTime())
                    .likeTargetId(likeTargetId)
                    .likeTargetType(likeTargetType)
                    .comment(comment)
                    .commentTargetId(commentTargetId)
                    .commentTargetType(commentTargetTypeEnum)
                    .build();
            likeMessageVOS.add(VO);
        }
        // 获取完以后要把这些like消息标为已读
        LambdaUpdateWrapper<LikeDO> likeDOUpdateWrapper = Wrappers.<LikeDO>lambdaUpdate()
                .set(LikeDO::getIsRead, IsReadEnum.IS_READ_TRUE.getType())
                .in(LikeDO::getId, likeIds);
        importMapperService.likeMapper.update(null, likeDOUpdateWrapper);
        return likeMessageVOS;
    }






}
